package com.sky.controller.admin;

import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.web.bind.annotation.*;

/**
 * 订单业务层
 *
 * @author normal
 * @date 2025年03月03日 下午7:41
 */
@RestController
@Slf4j
@RequestMapping("/admin/order")
@RequiredArgsConstructor
public class OrdersController {

    private final OrderService orderService;

    /**
     * 订单，根据条件搜索
     */
    @ApiOperation("订单根据条件查询")
    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult pages = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pages);
    }

    /**
     * 各个状态的订单数量统计 2待接单 3已接单 4派送中
     */
    @ApiOperation("各个状态的订单统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getOrderStatistics() {
        log.info("正在进行订单状态数量统计");
        OrderStatisticsVO vo = orderService.countOrdersStatus();
        return Result.success(vo);
    }

    /**
     *  查询订单详情
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> getOrderWithDetailById(@PathVariable Long id) {
        log.info("正在查询订单及其详情");
        OrderVO orderWithDetail = orderService.getOrderWithDetail(id);
        return Result.success(orderWithDetail);
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel")
    public Result<OrderVO> cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("正在进行取消订单:{}", ordersCancelDTO);
        orderService.cancelOrders(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 接单
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirmOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("正在进行接单:{}", ordersConfirmDTO);
        orderService.confirmedOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result rejectOrders(@RequestBody OrdersRejectionDTO rejectionDTO) {
        log.info("正在进行管理端拒单:{}", rejectionDTO);
        orderService.rejectOrders(rejectionDTO);
        return Result.success();
    }

    /**
     * 派送订单
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result delivery (@PathVariable Long id) {
        log.info("正在进行派送订单:id{}",id);
        orderService.deliveryOrders(id);
        return Result.success();
    }

    /**
     * 完成订单
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result completeOrders (@PathVariable Long id) {
        orderService.completeOrders(id);
        return Result.success();
    }
}
